﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class frmThaydoiquiche : Form
    {
        bool temp;       //tùy chọn insert or update
        string sql = "select * from T20_qui_dinh_luu_ban";
        SqlConnection connection = new SqlConnection("Data Source = .; Initial Catalog = SM; Integrated Security = True");
        public frmThaydoiquiche()
        {
            InitializeComponent();
        }

        DataSet Filldataset(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlDataAdapter adap = new SqlDataAdapter(sql, connection);
                adap.Fill(ds);
                adap.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex.Message);
            }
            return ds;
        }

        private void frmThaydoiquiche_Load(object sender, EventArgs e)
        {
            DataSet ds = Filldataset("select * from [T20_qui_dinh_luu_ban]");
            DataRow row = ds.Tables[0].Rows[0];
            txtDiemTB.Text = row["DiemDau"].ToString();
            txtTyle.Text = row["TiLeSoTrinhKhongDat"].ToString();
            txtLuuban.Text = row["DiemLuuBan"].ToString();
            txtLenlop.Text = row["DiemDau"].ToString();
            txtThoihoc.Text = row["DiemLuuBan"].ToString();
            lblID.Text = row["ID"].ToString();
        }
        public void query(string command)
        {

            connection.Open();
            SqlCommand sqlCommand = connection.CreateCommand();
            sqlCommand.CommandText = command;
            SqlDataReader dr = sqlCommand.ExecuteReader();
            connection.Close();
        }
        private void btnSua_Click_1(object sender, EventArgs e)
        {
            lblThongbao.Visible = true;
            btnLuu.Enabled = true;
            btnBoqua.Enabled = true;
            txtTyle.Enabled = true;
            txtDiemTB.Enabled = true;
            txtLuuban.Enabled = true;
            btnSua.Enabled = false;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnBoqua_Click_1(object sender, EventArgs e)
        {
            btnLuu.Enabled = false;
            btnBoqua.Enabled = false;
            txtTyle.Enabled = false;
            txtThoihoc.Enabled = false;
            txtDiemTB.Enabled = false;
            btnSua.Enabled = true;
        }

        private void btnLuu_Click_1(object sender, EventArgs e)
        {
            bool ktra = dxValidationProvider1.Validate();
            if (ktra)
            {
                double lb = Convert.ToDouble(txtLuuban.Text);
                double dtb = Convert.ToDouble(txtDiemTB.Text);
                if (lb < dtb)
                {
                    string str = "update T20_qui_dinh_luu_ban set TiLeSoTrinhKhongDat = N'" + txtTyle.Text + "', DiemLuuBan = N'" + txtLuuban.Text + "' , DiemDau = '" + txtDiemTB.Text + "' where ID = '" + lblID.Text + "'";
                    query(str);
                    btnLuu.Enabled = false;
                    btnBoqua.Enabled = false;
                    txtTyle.Enabled = false;
                    txtDiemTB.Enabled = false;
                    txtLuuban.Enabled = false;
                    txtThoihoc.Text = lb.ToString();
                    txtLenlop.Text = dtb.ToString();
                    btnSua.Enabled = true;
                    lblThongbao.Visible = false;
                }
                else
                    MessageBox.Show("Điểm trung bình phải lớn hơn điểm lưu ban", "Quản lý sinh viên", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
                MessageBox.Show("Bạn chưa nhập dữ liệu cần thiết", "Quản lý sinh viên", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
}
